package com.lancoo.answer.view.fragment.manual;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.paperUtils.FragmentLoadUtils;
import com.lancoo.answer.widget.combinationView.CompositionAnswerResultView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/8 11:52
 * Description:
 */
public class ManualAnswerLookingFragment extends Fragment {

    private Child child;
    private View convertView;
    private static final String TAG = "CompositionAnswerFragme";
    private int kindIndex;
    private int queseIndex;
    private int smallQueseIndex;
    private CompositionAnswerResultView compositionAnswerResultView;
    private RecyclerView recyclerView;
    private Ques ques;
    private int loadQueseIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        convertView=inflater.inflate(R.layout.ev_fragment_manu_answer_looking_layout,container,false);
        return convertView;
    }


    public static Fragment loadQueseFragment(int kindIndex, int queseIndex, int smallQueseIndex){
        Log.e("aaaa","ManualAnswerLookingFragment");
        ManualAnswerLookingFragment queseFragment=new ManualAnswerLookingFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    public static Fragment loadQueseFragment(int kindIndex, int queseIndex, int smallQueseIndex,Child child){

        ManualAnswerLookingFragment queseFragment=new ManualAnswerLookingFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){
        Log.e(TAG,"initView"+(child==null));
        if(getArguments()==null){
            return;
        }
        kindIndex = getArguments().getInt("kindIndex");
        queseIndex = getArguments().getInt("queseIndex");
        smallQueseIndex = getArguments().getInt("smallQueseIndex");
        loadQueseIndex = getArguments().getInt(AnswerConstant.KEY_LOADCHILDINDEX);
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        child = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex).getChildList().get(smallQueseIndex);
        ques = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex);

        QueseBodyView queseBodyView=view.findViewById(R.id.quese_body_view);
        queseBodyView.setEnableShowChildAsk(false);
        queseBodyView.initData(child);
        compositionAnswerResultView = view.findViewById(R.id.composition_answer_result_view);
        compositionAnswerResultView.setQueseID(ques.getQuesID());
        compositionAnswerResultView.setWriteResult(child,ques);
        loadQuesBodyFragment();

    }

    /**
     * 加载大题题干内容
     */
    private void loadQuesBodyFragment(){
        FragmentManager childFragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        Fragment fragment= FragmentLoadUtils.loadQuesBodyFragment(ques,kindIndex,queseIndex,smallQueseIndex,loadQueseIndex,0);
        fragmentTransaction.replace(R.id.framelayout_topicBody,fragment,TAG);
        fragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    public void onPause() {
        super.onPause();
        if(compositionAnswerResultView!=null){
            compositionAnswerResultView.pause();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(compositionAnswerResultView!=null){
            compositionAnswerResultView.release();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(compositionAnswerResultView!=null){
            compositionAnswerResultView.updateScoreState();
        }
    }
}
